<?php
/**
 *	description:ZMAXWECHAT 素材管理控制器文件
 *  author：min.zhang
 *  Email:zhang19min88@163.com
 *	Url:http://www.zmax99.com
 *  copyright:南宁市程序人软件科技有限责任公司保留所有权利
 *  date:2016-04-28
 *  @license GNU General Public License version 3, or later
 *  check date:2016-05-26
 *  checker:min.zhang
 */
 
defined('_JEXEC') or die('You can not access this file!');

jimport('joomla.application.component.controllerform');
require_once(JPATH_COMPONENT_ADMINISTRATOR."/libs/wechat/token.php");
require_once(JPATH_COMPONENT_ADMINISTRATOR."/libs/wechat/media.php");
require_once(JPATH_COMPONENT_ADMINISTRATOR."/libs/wechat/news.php");
require_once(JPATH_COMPONENT_ADMINISTRATOR."/libs/wechat/material.php");
require_once(JPATH_COMPONENT_ADMINISTRATOR."/libs/db.php");
class zmaxwechatControllerMedia extends JControllerForm
 {	
	public function save($key = NULL, $urlVar = NULL)
	{
		$app = JFactory::getApplication();
		$data = $app->input->get('jform','','ARRAY');
		
		$token = new zmaxwechatToken();
		$token = $token->getToken();
		
		$media = new zmaxwechatMedia($token->token);
		

		$news = array();
		for($i=0 ;$i<8;$i++)
		{
			$name="new".$i;
			if(isset($data[$name]) && is_array($data[$name]))
			{
				if($data[$name]["title"] && $data[$name]["content"])
				{
					$news[]=$data[$name];
				}
				
			}
		}
		
		$content = new stdclass();
		$content->news_item=$news;
		$content->create_time = JDate::getInstance()->toUnix();
		$content->update_time = JDate::getInstance()->toUnix();
		
		$data["content"] = json_encode($content);

		$id = 1;
		$article =JTable::getInstance("content");
		$article->load($id);
		
		
	
		
		$filePath = "/images/headers/windows.jpg";
		
		$material = new zmaxwechatMaterial($token->token);
		$info = $material->getMediaInfo($filePath);
		
		$new = new stdclass();
		$new->title=$article->title;
		$new->thumb_media_id = json_decode($info)->media_id;
		$new->author="ZMAX WECHAT";
		$new->digest=mb_strimwidth($article->introtext,0,40);
		$new->show_cover_pic=1;
		$new->content=$article->introtext.$article->fulltext;
		$new->content_source_url="http://www.joomlachina.cn";
		
		$news[]=$new;

		$result = $media->addNews($news);
		
		
		$filter = new stdclass();
		$filter->is_to_all=true;
		$filter->tag_id="";
		
		$news = new stdclass();
		$news->media_id = $result->media_id;
		
		$result = $media->sendNews($filter ,$news );
		

		

		
		
	}
 }	
	

?>